package code;

import java.util.ArrayList;
import java.util.List;

public class CodeList {

	private ArrayList<Code> currentCodeList = null;
	
	public CodeList() {
		currentCodeList = new ArrayList<Code>();
	}
	
	public void generateCode(Opcode opCode, double ... args) {
		currentCodeList.add(new Code(opCode, args));
	}
	
	public List<Code> getCodeAsList() {
		return this.currentCodeList;
	}
	
	public int getPosition() {
		return this.currentCodeList.size();
	}
	
	public int generateJump(Opcode op) {
		int res = getPosition();
		currentCodeList.add(new Code(op, res));
		return res;
	}
	
	public void patchJump(int positionOfJumpInstruction, int targetPosition) {
		double[] arr = new double[]{targetPosition-positionOfJumpInstruction};
		currentCodeList.get(positionOfJumpInstruction).setArgs(arr);
	}
}
